﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SysLog
    {
        public int Add(Chain.Model.SysLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysLog(");
            builder.Append("LogUserID,LogActionID,LogType,LogDetail,LogShopID,LogCreateTime,LogIPAdress)");
            builder.Append(" values (");
            builder.Append("@LogUserID,@LogActionID,@LogType,@LogDetail,@LogShopID,@LogCreateTime,@LogIPAdress)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogUserID", SqlDbType.Int, 4), new SqlParameter("@LogActionID", SqlDbType.Int, 4), new SqlParameter("@LogType", SqlDbType.NVarChar, 50), new SqlParameter("@LogDetail", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@LogShopID", SqlDbType.Int, 4), new SqlParameter("@LogCreateTime", SqlDbType.DateTime), new SqlParameter("@LogIPAdress", SqlDbType.VarChar, 50) };
            cmdParms[0].Value = model.LogUserID;
            cmdParms[1].Value = model.LogActionID;
            cmdParms[2].Value = model.LogType;
            cmdParms[3].Value = model.LogDetail;
            cmdParms[4].Value = model.LogShopID;
            cmdParms[5].Value = model.LogCreateTime;
            cmdParms[6].Value = model.LogIPAdress;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int CleadSysLog(int strDay)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(" delete from SysLog where LogCreateTime<=(dateadd(day,(-{0}),getdate()))", strDay);
            return DbHelperSQL.ExecuteSql(builder.ToString());
        }

        public bool DataBaseInit(ArrayList arrSql)
        {
            return DbHelperSQL.ExecuteSqlTran(arrSql);
        }

        public bool Delete(int LogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysLog ");
            builder.Append(" where LogID=@LogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LogID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string LogIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysLog ");
            builder.Append(" where LogID in (" + LogIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int LogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysLog");
            builder.Append(" where LogID=@LogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LogID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetActionList(string MemCard)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select top 1 * from SysLog");
            builder.AppendFormat(" where LogDetail like '%' + '{0}' + '%' ", MemCard);
            builder.Append(" and LogActionID>0");
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select LogID,LogUserID,LogActionID,LogType,LogDetail,LogShopID,LogCreateTime,LogIPAdress ");
            builder.Append(" FROM SysLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" LogID,LogUserID,LogActionID,LogType,LogDetail,LogShopID,LogCreateTime,LogIPAdress ");
            builder.Append(" FROM SysLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.LogID desc");
            }
            builder.Append(")AS Row, T.*  from SysLog T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "SysLog,SysShop,SysUser";
            string[] column = new string[] { "SysLog.*,SysShop.ShopName,SysUser.UserAccount,SysUser.UserName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "LogCreateTime", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("LogID", "SysLog");
        }

        public Chain.Model.SysLog GetModel(int LogID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 LogID,LogUserID,LogActionID,LogType,LogDetail,LogShopID,LogCreateTime,LogIPAdress from SysLog ");
            builder.Append(" where LogID=@LogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LogID;
            Chain.Model.SysLog log = new Chain.Model.SysLog();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["LogID"] != null) && (set.Tables[0].Rows[0]["LogID"].ToString() != ""))
            {
                log.LogID = int.Parse(set.Tables[0].Rows[0]["LogID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["LogUserID"] != null) && (set.Tables[0].Rows[0]["LogUserID"].ToString() != ""))
            {
                log.LogUserID = int.Parse(set.Tables[0].Rows[0]["LogUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["LogActionID"] != null) && (set.Tables[0].Rows[0]["LogActionID"].ToString() != ""))
            {
                log.LogActionID = int.Parse(set.Tables[0].Rows[0]["LogActionID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["LogType"] != null) && (set.Tables[0].Rows[0]["LogType"].ToString() != ""))
            {
                log.LogType = set.Tables[0].Rows[0]["LogType"].ToString();
            }
            if ((set.Tables[0].Rows[0]["LogDetail"] != null) && (set.Tables[0].Rows[0]["LogDetail"].ToString() != ""))
            {
                log.LogDetail = set.Tables[0].Rows[0]["LogDetail"].ToString();
            }
            if ((set.Tables[0].Rows[0]["LogShopID"] != null) && (set.Tables[0].Rows[0]["LogShopID"].ToString() != ""))
            {
                log.LogShopID = int.Parse(set.Tables[0].Rows[0]["LogShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["LogCreateTime"] != null) && (set.Tables[0].Rows[0]["LogCreateTime"].ToString() != ""))
            {
                log.LogCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["LogCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["LogIPAdress"] != null) && (set.Tables[0].Rows[0]["LogIPAdress"].ToString() != ""))
            {
                log.LogIPAdress = set.Tables[0].Rows[0]["LogIPAdress"].ToString();
            }
            return log;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.SysLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysLog set ");
            builder.Append("LogUserID=@LogUserID,");
            builder.Append("LogActionID=@LogActionID,");
            builder.Append("LogType=@LogType,");
            builder.Append("LogDetail=@LogDetail,");
            builder.Append("LogShopID=@LogShopID,");
            builder.Append("LogCreateTime=@LogCreateTime,");
            builder.Append("LogIPAdress=@LogIPAdress");
            builder.Append(" where LogID=@LogID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LogUserID", SqlDbType.Int, 4), new SqlParameter("@LogActionID", SqlDbType.Int, 4), new SqlParameter("@LogType", SqlDbType.NVarChar, 50), new SqlParameter("@LogDetail", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@LogShopID", SqlDbType.Int, 4), new SqlParameter("@LogCreateTime", SqlDbType.DateTime), new SqlParameter("@LogID", SqlDbType.Int, 4), new SqlParameter("@LogIPAdress", SqlDbType.VarChar, 50) };
            cmdParms[0].Value = model.LogUserID;
            cmdParms[1].Value = model.LogActionID;
            cmdParms[2].Value = model.LogType;
            cmdParms[3].Value = model.LogDetail;
            cmdParms[4].Value = model.LogShopID;
            cmdParms[5].Value = model.LogCreateTime;
            cmdParms[6].Value = model.LogID;
            cmdParms[7].Value = model.LogIPAdress;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

